# Contributor: Francesco Colista <francesco.colista@gmail.com>
# Maintainer: Nathan Angelacos <nangel@alpinelinux.org>
#
# NOTE: This is based on the sipwise "fork" of sems.

_basename=sems
pkgname=sipwise-$_basename
pkgver=3.8.2.1
pkgrel=2
pkgdesc="SIP Express Media Server, an extensible SIP media server"
url="http://iptel.org/sems/"
arch="all !aarch64"
license="GPL-2.0-or-later"
pkgusers="sems"
pkggroups="sems"
install="$pkgname.pre-install"
makedepends="python2-dev cmake bash openssl-dev linux-headers
	libsamplerate-dev spandsp-dev flite-dev gsm-dev speex-dev
	libevent-dev"
subpackages="$pkgname-doc $pkgname-conf_auth $pkgname-conference
	$pkgname-diameter_client $pkgname-dsm $pkgname-early_announce
	$pkgname-g722 $pkgname-gsm $pkgname-ivr $pkgname-mailbox
	$pkgname-pin_collect $pkgname-speex $pkgname-xmlrpc2di"

source="https://github.com/sipwise/sems/archive/mr$pkgver.tar.gz
	sems-0001-Force-to-use-system-wide-GSM-includes.patch
	sems-0002-Allow-rewrite-of-custom-makefiles-by-CMake.patch
	sems-0003-Set-CFG_PREFIX-to-empty-string-by-default.patch
	sems-0004-remove-execinfo-dependency.patch
	sems-0005-define-SEMS_VERSION.patch
	sems-0006-std-types.patch
	atomic-swap8.patch
	sems.initd
	"

#	sems-0005-Add-sw_prepaid_sip-plugin-to-default-exclude-list-du.patch
#	sems-0006-last-minute-fix-proper-version.patch
#	sems-0008-Install-email-template.patch
#	sems-0009-Don-t-start-py_sems-by-default.patch

# applied:
#	sems-0004-fix-PRACK-route.patch
#	sems-0007-Fix-gateway-module-s-version-passing-in-CMake.patch
#	sems-0010-Move-CMake-defines.patch

_builddir="$srcdir/$_basename-mr$pkgver"

prepare() {
	cd "$_builddir"
	rm -rf core/plug-in/gsm/gsm-1.0-pl10/
	for i in $source; do
		case $i in
		*.patch) msg $i; patch -p1 -i "$srcdir"/$i || return 1;;
		esac
	done
}


build() {
	cd "$_builddir"
	# force cmake to use relative paths. It helps us avoid having the
	# full file path in the logs
	cmake \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_USE_RELATIVE_PATHS=yes \
		-DSEMS_USE_SPANDSP=yes \
		-DSEMS_USE_LIBSAMPLERATE=yes \
		-DSEMS_USE_ZRTP=NO \
		-DSEMS_USE_MP3=NO \
		-DSEMS_USE_ILBC=NO \
		-DSEMS_USE_TTS=yes \
		-DSEMS_USE_OPENSSL=yes \
		-DSEMS_USE_MONITORING=yes \
		-DSEMS_USE_IPV6=NO \
		-DSEMS_CFG_PREFIX= \
		-DSEMS_AUDIO_PREFIX=/usr/share \
		-DSEMS_EXEC_PREFIX=/usr \
		-DSEMS_LIBDIR=lib \
		|| return 1
	make 
}

package() {
	cd "$_builddir"
	make install DESTDIR="$pkgdir"
	install -m755 -D "$srcdir"/$_basename.initd \
		"$pkgdir"/etc/init.d/$_basename || return 1
	# is empty
	rm -f "$pkgdir"/etc/sems/etc/conf_auth.conf

	install -o sems -g sems -d "$pkgdir"/var/spool/voicebox
	install -d -o sems  "$pkgdir"/var/run/sems
}

_mv_sub() {
	local i
	for i in "$@"; do
		mkdir -p "$subpkgdir"/${i%/*}
		mv "$pkgdir"/$i "$subpkgdir"/$i
	done
}

conf_auth() {
	pkgdesc="Conference with authorization for SEMS"
	depends="sems sems-ivr"
	cd "$pkgdir"
	_mv_sub usr/lib/sems/ivr/conf_auth*
}

conference() {
	pkgdesc="Conferencing application for SEMS"
	depends="sems"
	cd "$pkgdir"
	_mv_sub etc/sems/etc/conference.conf \
		usr/lib/sems/plug-in/conference.so \
		usr/share/sems/audio/conference
}

diameter_client() {
	pkgdesc="A simple DIAMETER client implementation for SEMS"
	depends="sems"
	cd "$pkgdir"
	_mv_sub usr/lib/sems/plug-in/diameter_client.so
}

dsm() {
	pkgdesc="The state machine interpreter for SEMS"
	depends="sems"
	cd "$pkgdir"
	_mv_sub etc/sems/etc/dsm* \
		usr/lib/sems/plug-in/dsm.so \
		usr/lib/sems/dsm
}

early_announce() {
	pkgdesc="Early announce application for SEMS"
	depends="sems"
	cd "$pkgdir"
	_mv_sub etc/sems/etc/early_announce.conf \
		usr/lib/sems/plug-in/early_announce.so
}

g722() {
	pkgdesc="G.722 support for SEMS"
	depends="sems"
	_mv_sub usr/lib/sems/plug-in/g722.so
}

#gateway() {
#	pkgdesc="ISDN gateway for SEMS"
#	depends="sems"
#}

gsm() {
	pkgdesc="GSM support for SEMS"
	depends="sems"
	_mv_sub usr/lib/sems/plug-in/gsm.so
}

ivr() {
	pkgdesc="IVR functionallity for SEMS"
	depends="sems python2"
	cd "$pkgdir"
	_mv_sub etc/sems/etc/ivr.conf \
		usr/lib/sems/plug-in/ivr.so \
		usr/lib/sems/ivr/log.*
}

mailbox() {
	pkgdesc="Mailbox application for SEMS"
	depends="sems sems-ivr"
	cd "$pkgdir"
	_mv_sub etc/sems/etc/mailbox*.conf \
		usr/share/sems/audio/mailbox \
		usr/lib/sems/plug-in/ivr.so \
		usr/lib/sems/ivr/*mailbox*
}

pin_collect() {
	pkgdesc="Collects a PIN for SEMS"
	depends="sems sems-ivr"
	cd "$pkgdir"
	_mv_sub etc/sems/etc/pin_collect.conf \
		usr/share/sems/audio/pin_collect \
		usr/lib/sems/plug-in/ivr.so \
		usr/lib/sems/ivr/pin_collect*
}

_python() {
	pkgdesc="Python bindings for SEMS"
	depends="python2 sems"
	cd "$pkgdir"
	_mv_sub \
		usr/lib/sems/plug-in/py_sems*
}

speex() {
	pkgdesc="Speex support for SEMS"
	depends="sems"
	_mv_sub usr/lib/sems/plug-in/speex.so
}

xmlrpc2di() {
	pkgdesc="XMLRPC interface for SEMS"
	depends="sems"
	_mv_sub \
		etc/sems/etc/xmlrpc2di.conf \
		usr/lib/sems/plug-in/xmlrpc2di.so
}


md5sums="8799f9be9161ee2024a10d2d36d4d0ae  mr3.8.2.1.tar.gz
5b2fded836b44bda33f39adc37de4de7  sems-0001-Force-to-use-system-wide-GSM-includes.patch
b8efd1a6415afa08f7c676f6976fca36  sems-0002-Allow-rewrite-of-custom-makefiles-by-CMake.patch
331e7529833c906b4738006f32a65fde  sems-0003-Set-CFG_PREFIX-to-empty-string-by-default.patch
80cffaa7c8963b6619a69133a9ffc2f8  sems-0004-remove-execinfo-dependency.patch
a680e142ee9b83e6d34fd0757940c5cc  sems-0005-define-SEMS_VERSION.patch
bbcbee0f73342b4c35deff15c1d708d5  sems-0006-std-types.patch
fe19aad863d6a0f3ed3edc4b771eb869  atomic-swap8.patch
4b537f18cda6c72d7afda582a6f5858e  sems.initd"
sha256sums="29c5c2999bac9d675dff9721f279d00d4becf31623deda502872c8c941e1ec0d  mr3.8.2.1.tar.gz
a36950f68e78af21f0e1152bb01e2f25f74b8c63210eecc01e4a4f90cf841371  sems-0001-Force-to-use-system-wide-GSM-includes.patch
0992f15e2a5c58d63a5a06dda027323e203fbf67d57022c11d5d63fd953088bc  sems-0002-Allow-rewrite-of-custom-makefiles-by-CMake.patch
04e0b2a944c5f64305e5711f5dfdcf4675fc7980225ef87267fcaff241168d46  sems-0003-Set-CFG_PREFIX-to-empty-string-by-default.patch
6e8d8a08004e44e603e32cb8e62809c7865b7e1c70bf66d37e8701dd308782d5  sems-0004-remove-execinfo-dependency.patch
0ecc448b3f90261af984bcd0c7ebd6e76e0e696b3c285b6c1837f9296a429b68  sems-0005-define-SEMS_VERSION.patch
c4aac6f50d8ba85d389a859d9067fb7e7589daf4b84cd1d68ac231bcdb0d2aeb  sems-0006-std-types.patch
08adfa5d87427b56fdec1239a98429ba0e851fcb4cd5c5c0b319aeef93e8ebe7  atomic-swap8.patch
5bc8ece09cf1c47c8f0880fdeca40d1e72eb4bc5c6356ffc2d738179b9463e19  sems.initd"
sha512sums="27ba6a7c2671a3c9af14b9c37c458641e8deced10f19047fdb29d7b4a5c775ec1a378d574c73f8b5546a34dc27a759109ed638de3aea9f28a0d06db1c54a2fcc  mr3.8.2.1.tar.gz
87c6f878c3ad5e947a9a80431ddf28f08c04ce1415b7c42878f66b423c78d909efda836c7c2d3ed97e075c08d7a07c4707a49c14ce42c1a3aa350de1878c8d09  sems-0001-Force-to-use-system-wide-GSM-includes.patch
4c09033d6acd1bbef2aa2d02571f74da29532decca67b9ce861872d8d9522ca31808d6e26c3b536913ef7a988f90d374e217187b9cadb6a79d5c830571e94b67  sems-0002-Allow-rewrite-of-custom-makefiles-by-CMake.patch
ba6f0d917464729c63628255bee3d864a954af4a510b802f0de1d51f35ff5d7c1c33cbe14db31818133a3c94f84da0f4bea4184ce1e8eae6aa9a1ba79f2e5347  sems-0003-Set-CFG_PREFIX-to-empty-string-by-default.patch
b23f47640e495513abc85a9ae08b8ff7e2fbc9b57d072eb9b0b3875b9141ee9e67f1aebdbe3f2b09447dbae0e179490f2de7390da07127919ddf85c1a47c4e5c  sems-0004-remove-execinfo-dependency.patch
02574aeb5b94f4a549fe71e1a8a0efad950d66dfd57ed2ee47befb8aa1c508dd6b05a55586ef1d237e9d61e64aa27fcd8689dc86552e69aff43aa4c96ea2245b  sems-0005-define-SEMS_VERSION.patch
79e3ac2b202b3f6c710b3d2bc8ac459e3622f2bf5945eaaa81778f53bcbddc7951266f88c2fd9862fbed44f10a160368af7d19b5e81929a3b1e8ee150111f74b  sems-0006-std-types.patch
504f91d1115e5e15d1724957b1205fcb9e82e9059bc8b480868bffb5e5b4363ff312d6deb23f6204164c4ec1d07d63312e9484f5b6c289b159fb86abe3b99037  atomic-swap8.patch
5be93b6444a9592daa6c883c78e5906e9e662891e1daa850d872bdaa657eb756f1bc0879732033479b279b75c2fd42ef79aa530ebf3b1692e7fb222d484f804b  sems.initd"
